package com.cjgl.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cjgl.common.BaseResponse;
import com.cjgl.common.ErrorCode;
import com.cjgl.common.ResultUtils;
import com.cjgl.exception.BusinessException;
import com.cjgl.model.dto.courseTable.SelectByCourseNoOrCourseNameOrTeacherOrTimeRequset;
import com.cjgl.model.dto.score.SelectScoresByTermOrCourseNameREquest;
import com.cjgl.model.entity.Coursestableview;
import com.cjgl.model.enums.LengthTypeEnum;
import com.cjgl.service.CoursestableviewService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhangyuwu
 */
@RestController
@RequestMapping("/adminManageCourseTableInfo")
public class AdminManageCourseTableInfo {

	@Resource
	CoursestableviewService coursestableviewService;


	/**
	 * authTime不填
	 * 课表信息查询 根据课程号 课程名 教师名 教师号 班级号 地点 学期 教授时间（可以到天accurateToDay=year+month+day）
	 *
	 * @param coursesSelectCon
	 * @param request
	 * @return
	 */
	@PostMapping
	public BaseResponse<List<Coursestableview>> getCourseTableInfo(@RequestBody SelectScoresByTermOrCourseNameREquest coursesSelectCon,
																   HttpServletRequest request) {

		String courseNo = coursesSelectCon.getCourseNo() == null ? "" : coursesSelectCon.getCourseNo();
		String courseName = coursesSelectCon.getCourseName() == null ? "" : coursesSelectCon.getCourseName();
		String teacherName = coursesSelectCon.getTeacherName() == null ? "" : coursesSelectCon.getTeacherName();
		String teacherNo = coursesSelectCon.getTeacherNo() == null ? "" : coursesSelectCon.getTeacherNo();
		String clazzNo = coursesSelectCon.getClazzNo() == null ? "" : coursesSelectCon.getClazzNo();
		String place = coursesSelectCon.getPlace() == null ? "" : coursesSelectCon.getPlace();

		String ter = coursesSelectCon.getTer() == null ? "" : coursesSelectCon.getTer();
		//year默认4位长度
		String year = coursesSelectCon.getYear() == null ? "" : coursesSelectCon.getYear();
		String courseTeachingYear = coursesSelectCon.getCourseTeachingYear() == null ? "" : coursesSelectCon.getCourseTeachingYear();
		String month = coursesSelectCon.getMonth() == null ? "" : coursesSelectCon.getMonth();
		String day = coursesSelectCon.getDay() == null ? "" : coursesSelectCon.getDay();

		if (month.length() != LengthTypeEnum.MONTH_LENGTH.getValue() && month.length() == 1) {
			month = "-0" + month;
		}

		if (day.length() != LengthTypeEnum.DAY_LENGTH.getValue() && month.length() == 1) {
			day = "-0" + day;
		} else if (day.length() == LengthTypeEnum.DAY_LENGTH.getValue()) {
			day = "-" + day;
		}

		String accurateToDay = courseTeachingYear + month + day;
		String termSelect = year + ter;


		LambdaQueryWrapper<Coursestableview> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper
				.like(!StringUtils.isEmpty(termSelect),Coursestableview::getTerm,termSelect)
				.like(!StringUtils.isEmpty(courseNo),Coursestableview::getCourseNo,courseNo)
				.like(!StringUtils.isEmpty(courseName),Coursestableview::getCourseName,courseName)
				.like(!StringUtils.isEmpty(teacherName),Coursestableview::getTeacherName,teacherName)
				.like(!StringUtils.isEmpty(teacherNo),Coursestableview::getTeacherNo,teacherNo)
				.like(!StringUtils.isEmpty(clazzNo),Coursestableview::getClassNo,clazzNo)
				.like(!StringUtils.isEmpty(place),Coursestableview::getPlace,place)
				.like(!StringUtils.isEmpty(accurateToDay),Coursestableview::getTeachingTime,accurateToDay);

		List<Coursestableview> coursestableviewList = coursestableviewService.getBaseMapper().selectList(queryWrapper);

		return ResultUtils.success(coursestableviewList);
	}

	// TODO 排课控制：一名教师不能同时给两个班级上课，一个班不能同时上两门课，一个教室不能同时排两门课
}
